/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package 解析.node.node;

import 解析.node.others.TreeInfo;
import 解析.Matcher;

/**
 *
 * @author yanxi
 */
public class Conditional extends Node {

    public Node cond, yes, not;

    Conditional(Node cond, Node yes, Node not) {
        this.cond = cond;
        this.yes = yes;
        this.not = not;
        
        getChildren().add(cond);
        getChildren().add(yes);
        getChildren().add(not);
    }

    @Override
    public boolean match(Matcher matcher, int i, CharSequence seq) {
        if (cond.match(matcher, i, seq)) {
            return yes.match(matcher, i, seq);
        } else {
            return not.match(matcher, i, seq);
        }
    }

    @Override
    public boolean study(TreeInfo info) {
        int minL = info.minLength;
        int maxL = info.maxLength;
        boolean maxV = info.maxValid;
        info.reset();
        yes.study(info);

        int minL2 = info.minLength;
        int maxL2 = info.maxLength;
        boolean maxV2 = info.maxValid;
        info.reset();
        not.study(info);

        info.minLength = minL + Math.min(minL2, info.minLength);
        info.maxLength = maxL + Math.max(maxL2, info.maxLength);
        info.maxValid = (maxV & maxV2 & info.maxValid);
        info.deterministic = false;
        return next.study(info);
    } 
}
